home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 November / Chip Kasım 2001.iso / prog / cdcode / smartsdk / setup.exe / stsdk.msi / STRecognizer.cpp.C6DB95E3_9157_4174_9574_EF0371EC54EB < prev    next >
Encoding:
Text File  |  2001-01-18  |  5.2 KB  |  165 lines

  1. // STRecognizer.cpp : Implementation of CSTRecognizer
  2. #include "stdafx.h"
  3. #include "SimpleTerm2.h"
  4. #include "STRecognizer.h"
  5. #include "common.h"
  6.  
  7. /////////////////////////////////////////////////////////////////////////////
  8. // CSTRecognizer
  9.  
  10. // 'TERM' is a data structure that will associate the 
  11. // coffee flavor (strValue) with it's product ID (strProductID).   
  12. typedef struct _term
  13. {
  14.     WCHAR *strValue;
  15.     WCHAR *strProductID;
  16. } TERM;
  17.  
  18. // Build and populate the array with a list of strings of terms to be 
  19. // recognized, linked to their corresponding ProductID.  Declare these terms 
  20. // in all lowercase to allow case-insensitive searches to be performed.
  21. TERM arrTerm[] = {
  22.     {L"cappuccino", L"CAP"},
  23.     {L"caramel macchiato", L"CAR"},
  24.     {L"mocha", L"MOC"},
  25.     {L"americano", L"AME"},
  26.     {L"breve", L"BRE"},
  27.     {L"white chocolate mocha", L"WCM"},
  28.     {L"espresso con panna", L"ECP"},
  29.     {L"coffee shake", L"CS"},
  30.     {L"iced caffe mocha", L"ICM"},
  31.     {L"iced caffe latte", L"ICL"}
  32. };
  33.  
  34. // cTerms is the size of the arrTerm array.
  35. // Alternately you can also #define cTerms (sizeof(arrTerm)/sizeof(TERM))
  36. const int cTerms = 10;  
  37.  
  38. //A helper function for string allocation methods that other functions
  39. //could share since the method within these functions are similar.  
  40. HRESULT HrAllocString(BSTR *pbstr, WCHAR *sz)
  41. {
  42.     if (pbstr == NULL)
  43.         return E_POINTER;  // Invalid pointer.
  44.             
  45.     *pbstr    = SysAllocString(sz);
  46.     if (*pbstr == NULL)
  47.         return E_OUTOFMEMORY;  // The function failed to allocate the 
  48.                                // necessary memory.
  49.  
  50.     return S_OK;  // Successful operation.
  51. }
  52.  
  53. // Create a unique identifier for this recognizer 
  54. // that corresponds to the ProgID of this dll.
  55. STDMETHODIMP CSTRecognizer::get_ProgId (BSTR * ProgId)
  56. {
  57.     return HrAllocString(ProgId, L"SimpleTerm2.STRecognizer");
  58. }
  59.  
  60. // Create a check box caption that will be 
  61. // displayed in the Tools>Autocorrect Option>Smart Tags 
  62. // dialog box in both Word and Excel.
  63. STDMETHODIMP CSTRecognizer::get_Name (INT LocaleID, BSTR * Name)
  64. {
  65.     return HrAllocString(Name, L"My Simple Term2 Recognizer"); 
  66. }
  67.  
  68. // Create a short phrase that describes this recognizer. 
  69. STDMETHODIMP CSTRecognizer::get_Desc (INT LocaleID, BSTR * Desc)
  70. {
  71.     return HrAllocString(Desc, L"Simple Term2 Recognizer recognizes Fourth Coffee2 flavors in documents");
  72. }
  73.  
  74. // Register the number of smart tag types this recognizer supports. 
  75. // 1 in this case.
  76. STDMETHODIMP CSTRecognizer::get_SmartTagCount (INT * Count)
  77. {
  78.     if (Count == NULL)
  79.         return E_POINTER;
  80.             
  81.     *Count = 1;
  82.     return S_OK;
  83. }
  84.  
  85. // Provide the name of the smart tag type to be supported.  
  86. // Smart Tag type names are always in the format of namespaceURI#tagname.      
  87. STDMETHODIMP CSTRecognizer::get_SmartTagName (INT SmartTagID, BSTR * Name)
  88. {
  89.     // SmartTagID is an enumerated integer that corresponds to 
  90.     // SmartTagCount.  The count will go from 1 to SmartTagCount.
  91.     if (SmartTagID != 1)
  92.         return E_INVALIDARG;  // One or more arguments are invalid.
  93.  
  94.     // SMART_TAG_SCHEMA is #defined in common.h
  95.     // In this example, only one smart tag type is supported. 
  96.     return HrAllocString(Name, SMART_TAG_SCHEMA);
  97.     
  98. }
  99.  
  100. // Since for this particular smart tag type there is no website to 
  101. // support a smart tag download URL, none will be provided, so return null.
  102. STDMETHODIMP CSTRecognizer::get_SmartTagDownloadURL (INT SmartTagID, BSTR * DownloadURL)
  103. {
  104.     if (DownloadURL == NULL)
  105.         return E_POINTER;
  106.             
  107.     *DownloadURL = NULL;
  108.     return S_OK;
  109. }
  110.  
  111. // Search for strings in TERM arrTerm[] array. This will perform 
  112. // a case-insensitive search through the passed-in string for each 
  113. // of the terms supplied. 
  114. STDMETHODIMP CSTRecognizer::Recognize (BSTR Text, IF_TYPE DataType, INT LocaleID, ISmartTagRecognizerSite * RecognizerSite)
  115. {    
  116.     int iTerm;
  117.     WCHAR *pch, *strText;
  118.     ISmartTagProperties  *pSmartTag = NULL;
  119.     BSTR bstrFac, bstrKey, bstrValue;
  120.  
  121.     strText = (WCHAR *)Text;
  122.  
  123.     // For simplicity, the check will only be until the first '\0' character.
  124.     // Here, the string is made lowercase to check against a list of terms.
  125.     wcslwr(strText);
  126.  
  127.     // Loop through the terms.
  128.     for(iTerm = 0; iTerm < cTerms; iTerm++)
  129.     {
  130.         // Find each occurrence of arrTerm[iTerm] within the given Text.
  131.         for (pch = strText; (pch = wcsstr(pch, arrTerm[iTerm].strValue)) != NULL; pch++)
  132.         {    
  133.             bstrFac = SysAllocString(SMART_TAG_SCHEMA);
  134.             if (bstrFac != NULL)
  135.             {
  136.                 // Create a new property bag for the smart tag.
  137.                 RecognizerSite->GetNewPropertyBag(&pSmartTag);
  138.                 if (pSmartTag != NULL)
  139.                 {
  140.                     bstrKey = SysAllocString(PRODUCT_ID);
  141.                     bstrValue = SysAllocString(arrTerm[iTerm].strProductID);
  142.                     // Populate the property bag by writing a key into the
  143.                     // property bag.  If the key name doesn't exist in the
  144.                     // property bag a new property is created.
  145.                     if (bstrKey != NULL && bstrValue != NULL)
  146.                         pSmartTag->Write(bstrKey, bstrValue);
  147.                     SysFreeString(bstrKey);
  148.                     SysFreeString(bstrValue);
  149.                 }
  150.  
  151.                 // The smart tag will still be committed even if the
  152.                 // property bag could not be allocated or filled.
  153.                 RecognizerSite->CommitSmartTag(bstrFac, pch - strText + 1, wcslen(arrTerm[iTerm].strValue), pSmartTag);
  154.                 
  155.                 if (pSmartTag != NULL)
  156.                     pSmartTag->Release();
  157.  
  158.                 SysFreeString(bstrFac);
  159.             }
  160.         }
  161.     }
  162.     
  163.     return S_OK;        
  164. }
  165.